const calcAccuracySummary = (tempAccuracyData) => {
  // 验证输入数据的合法性
  if (!Array.isArray(tempAccuracyData)) {
    throw new Error('输入数据不是数组')
  }
  tempAccuracyData.forEach((item, index) => {
    if (item !== 0 && item !== 1) {
      throw new Error(`第 ${index + 1} 个数据项无效，必须是 0 或 1，实际值为: ${item}`)
    }
  })

  // 初始化统计变量
  let correctCount = 0 // 正确次数
  let incorrectCount = 0 // 错误次数
  let maxConsecutiveCorrect = 0 // 最大连续正确次数
  let maxConsecutiveIncorrect = 0 // 最大连续错误次数
  let currentConsecutiveCorrect = 0 // 当前连续正确次数
  let currentConsecutiveIncorrect = 0 // 当前连续错误次数
  let consecutiveCorrectSegments = 0 // 连续正确段数
  let consecutiveIncorrectSegments = 0 // 连续错误段数
  let totalConsecutiveCorrect = 0 // 总连续正确次数，用于计算平均值
  let totalConsecutiveIncorrect = 0 // 总连续错误次数，用于计算平均值

  // 遍历每个结果
  tempAccuracyData.forEach((result, index) => {
    if (result === 1) {
      // 处理正确的情况
      correctCount++
      if (currentConsecutiveIncorrect > 0) {
        // 更新最大连续错误次数并记录错误段
        maxConsecutiveIncorrect = Math.max(maxConsecutiveIncorrect, currentConsecutiveIncorrect)
        totalConsecutiveIncorrect += currentConsecutiveIncorrect
        consecutiveIncorrectSegments++
        currentConsecutiveIncorrect = 0
      }
      currentConsecutiveCorrect++
    } else {
      // 处理错误的情况
      incorrectCount++
      if (currentConsecutiveCorrect > 0) {
        // 更新最大连续正确次数并记录正确段
        maxConsecutiveCorrect = Math.max(maxConsecutiveCorrect, currentConsecutiveCorrect)
        totalConsecutiveCorrect += currentConsecutiveCorrect
        consecutiveCorrectSegments++
        currentConsecutiveCorrect = 0
      }
      currentConsecutiveIncorrect++
    }
  })

  // 检查最后的连续段
  if (currentConsecutiveCorrect > 0) {
    maxConsecutiveCorrect = Math.max(maxConsecutiveCorrect, currentConsecutiveCorrect)
    totalConsecutiveCorrect += currentConsecutiveCorrect
    consecutiveCorrectSegments++
  }
  if (currentConsecutiveIncorrect > 0) {
    maxConsecutiveIncorrect = Math.max(maxConsecutiveIncorrect, currentConsecutiveIncorrect)
    totalConsecutiveIncorrect += currentConsecutiveIncorrect
    consecutiveIncorrectSegments++
  }

  // 计算平均连续正确和错误次数
  const avgConsecutiveCorrect =
    consecutiveCorrectSegments > 0 ? totalConsecutiveCorrect / consecutiveCorrectSegments : 0
  const avgConsecutiveIncorrect =
    consecutiveIncorrectSegments > 0 ? totalConsecutiveIncorrect / consecutiveIncorrectSegments : 0

  // 计算准确率
  const accuracyRate = correctCount + incorrectCount > 0 ? correctCount / (correctCount + incorrectCount) : 0

  // 返回计算结果
  return {
    count: correctCount + incorrectCount,
    // 正确次数
    correctCount,
    // 错误次数
    incorrectCount,
    // 准确率
    accuracyRate,
    // 最大连续正确次数
    maxConsecutiveCorrect,
    // 最大连续错误次数
    maxConsecutiveIncorrect,
    // 连续正确段数
    consecutiveCorrectSegments,
    // 连续错误段数
    consecutiveIncorrectSegments,
    // 平均连续正确次数
    avgConsecutiveCorrect,
    // 平均连续错误次数
    avgConsecutiveIncorrect,
    // 当前连续正确次数
    currentConsecutiveCorrect,
    // 当前连续错误次数
    currentConsecutiveIncorrect
  }
}

const ACCURACY_SUMMARY_CN_MAP = {
  count: '统计次数',
  correctCount: '正确次数',
  incorrectCount: '错误次数',
  accuracyRate: '准确率',
  maxConsecutiveCorrect: '最大连续正确次数',
  maxConsecutiveIncorrect: '最大连续错误次数',
  consecutiveCorrectSegments: '连续正确段数',
  consecutiveIncorrectSegments: '连续错误段数',
  avgConsecutiveCorrect: '平均连续正确次数',
  avgConsecutiveIncorrect: '平均连续错误次数',
  currentConsecutiveCorrect: '当前连续正确次数',
  currentConsecutiveIncorrect: '当前连续错误次数'
}

module.exports = {
  calcAccuracySummary,
  ACCURACY_SUMMARY_CN_MAP
}
